use std:: {fs};
use std::error::Error;

pub struct Config {
    query : String,
    filename : String,
}

impl Config {
    pub fn new(args: &[String]) -> Config {
        if args.len() < 3 {
            panic!("No enough args!")
        }

        let query = args[1].clone();
        let filename = args[2].clone();

        Config{ query : query , filename : filename, }
    }

    pub fn run(&self) -> Result<(),Box<dyn Error>> {
        let context = fs::read_to_string(self.filename.clone())?;

        // println!("context is : {}" ,context);

        for line in Config::search(&self.query, &context).iter() {
            println!("{}", line);
        }    

        Ok(())
    }

    fn search(query: &str, contents: &str) -> Vec<String> {
        let mut results : Vec<String> = Vec::new();
    
        for line in contents.lines() {
            if line.contains(query) {
                results.push(String::from(line));
            }
        }
    
        results
    }
}


